package com.gopay.scheduler.job;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.gopay.common.constants.cbrmb.CbRmb01743BizStep;
import com.gopay.common.constants.proccode.ProcCodeConstants;
import com.gopay.common.domain.cps.intlcard.Tran01743VO;
import com.gopay.common.domain.cps.vo.CPSResponse;
import com.gopay.common.exception.GopayException;
import com.gopay.scheduler.GopayBaseJob;
import com.gopay.scheduler.common.CallCPSHelper;
import com.gopay.scheduler.service.cbrmb.CbRmbIntlCardService;

/**
 * 跨境外卡支付拒付调单扣款job
 * 
 * @ClassName: CbRmbIntlCardDishonorRetrievalOrderJob 
 * @author lailingyu
 * @date 2017年11月8日 下午2:40:44
 */
public class CbRmbIntlCardDishonorRetrievalOrderJob extends GopayBaseJob {

	private Logger logger = LoggerFactory.getLogger(this.getClass());

	@Resource(name = "cbRmbIntlCardService")
	private CbRmbIntlCardService cbRmbIntlCardService;

    @Resource(name = "callCPSHelper")
    private CallCPSHelper callCPSHelper;

	@Override
	protected void doJobInternal() {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS");
		logger.info("-------跨境人民币外卡支付拒付调单扣款定时任务开始，时间：" + sdf.format(new Date(System.currentTimeMillis())) + "--------");
		List<String[]> orderList = cbRmbIntlCardService.getDishonorOrders();
		if (orderList.size() > 0) {
			for (String[] order : orderList) {
				Tran01743VO tranVo = new Tran01743VO();
				tranVo.setBizStep(CbRmb01743BizStep.CRON);
				tranVo.setGopayOrderId(order[0]);
				try {
					CPSResponse response = callCPSHelper.callCbRmbByVOTicket(tranVo);
					if (!ProcCodeConstants.PROC_CODE_100S1000.equals(response.getRespCode())) {
						logger.error("-------跨境人民币外卡支付拒付调单扣款定时任务失败， RespCode:" + response.getRespCode() + "，订单号：" + order[0] + "--------");
					} else {
						logger.info("-------跨境人民币外卡支付拒付调单扣款定时任务成功，订单号：" + order[0] + "--------");
					}
				} catch (GopayException e) {
					logger.error("-------跨境人民币外卡支付拒付调单扣款定时任务失败，错误信息：" + e.getErrMsg() + "，订单号：" + order[0] + "--------");
				}
			}
			logger.info("-------跨境人民币外卡支付拒付调单扣款定时任务结束，时间：" + sdf.format(new Date(System.currentTimeMillis())) + "--------");
		}
	}
}
